package org.zcl.sqlserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.sqlserver.entity.Notice;
import org.zcl.sqlserver.service.NoticeService;

import java.util.HashMap;
import java.util.Map;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/7/19 14:58
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @GetMapping("list")
    public String list(HashMap map, String nameParam,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            JSONObject jsonObject = (JSONObject) JSON.toJSON(noticeService.findList(nameParam, page - 1, pageSize));
            System.out.println(jsonObject);
            map.put("datas", jsonObject);
            map.put("nameParam", nameParam);
            map.put("code", 1);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "失败");
            e.printStackTrace();
        }
        return "notice_list";
    }

    @PostMapping("toEdit")
    public Map toEdit(Long id) {
        Map map = new HashMap();
        try {
            map.put("bean", noticeService.findById(id));
            map.put("code", 1);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "失败");
            e.printStackTrace();
        }
        return map;
    }

    @PostMapping("edit")
    public Map editMenu(Notice bean) {
        Map map = new HashMap();
        try {
            return noticeService.edit(bean);
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "失败");
            e.printStackTrace();
            return map;
        }
    }

    @PostMapping("deleteById")
    public Map deleteById(Long id) {
        return noticeService.deleteById(id);
    }

    @GetMapping("listCount")
    public Map listCount() {
        Map map = new HashMap();
        try {
            map.put("count", noticeService.listCount());
            map.put("code", 1);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "失败");
            e.printStackTrace();
        }
        return map;
    }
}
